﻿using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using SharpDX;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Input;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class TaskJobViewModel : ListRegionViewModel<TaskJob>
    {
        private ITaskJobService _taskJobService;
        private DelegateCommand _downloadFileCommand;
        private DelegateCommand _deleteTaskCommand;

        public TaskJobViewModel(
            IRegionManager regionManager,
            IUnityContainer unityContainer,
            IDialogService dialogService,
            ITaskJobService taskJobServices
            ) :
            base(regionManager, unityContainer, dialogService)
        {
            _taskJobService = taskJobServices;
        }
        public ICommand DownloadFileCommand
        {
            get
            {
                if (_downloadFileCommand != null) return _downloadFileCommand;
                _downloadFileCommand = new DelegateCommand(onDownLoadCommandAsync);
                return _downloadFileCommand;
            }
        }
        public ICommand DeleteTaskCommand
        {
            get
            {
                if (_deleteTaskCommand != null) return _deleteTaskCommand;
               _deleteTaskCommand = new DelegateCommand(onDeleteTaskCommandAsync);
                return _deleteTaskCommand;
            }
        }
        protected override async void OnInitPage(object obj)
        {
            try
            {
                string proId = GlobalObject.CurrentProject.ProjId.ToString();
                var list = await _taskJobService.GetLisAsync(proId);
                DataList.Clear();
                DataList.AddRange(list);
            }
            catch (Exception x)
            {
                var xMessage = $"模板数据加载失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }

        private async void onDeleteTaskCommandAsync()
        {
            if (SelectObj == null)
            {
                Dialog.ShowAltert("请先选择需要删除的任务！！！");
                return;
            }
            string proId = GlobalObject.CurrentProject.ProjId.ToString();
            try
            {
                var result = await _taskJobService.DeleteTaskJobAsync(proId, SelectObj.ID.ToString());
                if (!result.succeeded)
                {
                    LoggerHelper.Current.Error(result.errors.ToString());
                    Dialog.ShowAltert(result.errors.ToString());
                    return;
                }

                DataList.Remove(SelectObj);//删除成功，更新列表
            }
            catch (Exception x)
            {

                var xMessage = $"删除模板失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            } 
        }

        private async void onDownLoadCommandAsync()
        {
            if (SelectObj == null)
            {
                Dialog.ShowAltert("请先选择需要下载的任务！！！");
                return;
            }
            string proId = GlobalObject.CurrentProject.ProjId.ToString();

            try
            {
                RESTfulResult<Stream> result = new RESTfulResult<Stream>();

                result = await _taskJobService.GetFileAsync(proId, SelectObj);

                if (!result.succeeded)
                {
                    LoggerHelper.Current.Error(result.errors.ToString());
                    Dialog.ShowAltert(result.errors.ToString());
                    return;
                }
                var saveFileDialog = new Microsoft.Win32.SaveFileDialog
                {
                    FileName = SelectObj.FILE_NAME
                };
                using var stream = result.data;
                if (saveFileDialog.ShowDialog() == true)
                {
                    var _fielPath = saveFileDialog.FileName;
                    await FileHelper.WriteFileStreamAsync(stream, _fielPath);
                    var rMessage = "下载模板文件到本地成功！！！";
                    Dialog.ShowAltert(rMessage);
                    ExplorerFileExtensions.ExplorerFile(_fielPath); //打开目录
                    return;
                }
            }
            catch (Exception x)
            {
                var xMessage = $"获取远程服务器数据失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
